<?php

namespace App\Http\Controllers\Game;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Resources\Common\DatasResource;
use App\Http\Resources\Common\DataResource;
use App\Services\Postman\GameService as PostmanGameService;
use App\Services\Game\UserService;

class UserController extends Controller
{

    /**
     * 导出数据
     */
    public function export(Request $request)
    {
        UserService::create()->getList($request, true);

    }

    /**
     * 获取数据列表
     */
    public function getList(Request $request)
    {
        list ($collection, $total, $stats) = UserService::make()->getList($request);

        return DatasResource::make($collection)->setPaginateTotal($total)->setAttribute('stats', $stats);
    }

    public function getStat(Request $request)
    {
        list ($stats) = UserService::make()->getStat($request);

        return new DataResource($stats);
    }

    /**
     * 更新数据【Role】
     */
    public function updateRole(Request $request)
    {
        $data = $this->validate($request, [
            'id' => 'required|integer|min:1',
            'role' => 'required|array|min:1',
        ]);

        PostmanGameService::post(PostmanGameService::URI_SET_PLAYER_ROLE,
            ['uid' => $data['id'], 'role' => array_sum($data['role'])]
        );

        return new DataResource($data);
    }

    /**
     * 更新数据【Access】
     */
    public function updateAccess(Request $request)
    {
        $data = $this->validate($request, [
            'id' => 'required|integer|min:1',
            'access' => 'required|array|min:1',
        ]);

        PostmanGameService::post(PostmanGameService::URI_SET_PLAYER_ACCESS,
            ['uid' => $data['id'], 'access' => array_sum($data['access'])]
        );

        return new DataResource($data);
    }

    /**
     * 更新数据【Model】【slo】
     */
    public function updateModel(Request $request)
    {
        $data = $this->validate($request, [
            'id' => 'required|integer|min:1',
            'slo_model_id' => 'required|integer|min:1',
        ]);

        if (in_array($data['slo_model_id'], [2, 3]) && !$request->user()->can('menuAction.game.user.model')) {
            abort(403);
        }

        PostmanGameService::post(PostmanGameService::URI_SET_PLAYER_MODEL,
            ['uid' => $data['id'], 'model_id' => $data['slo_model_id']]
        );

        return new DataResource($data);
    }

    /**
     * 更新数据【Ratio】【fish】
     */
    public function updateFishRatio(Request $request)
    {
        $data = $this->validate($request, [
            'id' => 'required|integer|min:1',
            'fish_model_id' => 'required|integer|min:1',
        ]);

        if (in_array($data['fish_model_id'], [2, 3]) && !$request->user()->can('menuAction.game.user.ratio')) {
            abort(403);
        }

        PostmanGameService::post(PostmanGameService::URI_SET_PLAYER_FISH_RATIO,
            ['uid' => $data['id'], 'fish_model_id' => $data['fish_model_id']]
        );

        return new DataResource($data);
    }
}
